package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.CurrentUser;
import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.User;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private ITravelCommentService commentService;
    //游记明细
    @GetMapping("/detail")
    public Object detail(String id){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }
    //目的地下的游记列表
    @GetMapping("/query")
    public Object query(TravelQuery qo){
        Page<Travel> page = travelService.list(qo);
        return JsonResult.success(page);
    }

    //新增/编辑
    @RequireLogin
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel ,HttpServletRequest request){
        if (StringUtils.hasLength(travel.getId())){
            //带id则是编辑
            travelService.update(travel);
        }else {
            //没带则是新增
            //把保存需要的用户id处理了
            String token = request.getHeader("token");
            User currentUser = userService.getCurrentUser(token);
            travel.setUserId(currentUser.getId());
            travelService.save(travel);
        }
        //返回结果状态
        return JsonResult.success();
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    //游记编辑  页面要多个可区分的不同类型的值,用map映射
    @GetMapping("/input")
    public Object input(String id){
        HashMap<String, Object> map = new HashMap<>();
        //查询所有目的地给页面做下拉框
        List<Destination> list = destinationService.listAll();
        map.put("dests",list);
        //查询游记给编辑做回显
        if (id != null) {
            Travel travel = travelService.get(id);
            map.put("tv", travel);
        }
        return JsonResult.success(map);
    }

    //游记评论，要求登录，注入当前登录用户
    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment , @CurrentUser User user){
        commentService.save(travelComment , user);
        return JsonResult.success();
    }
    //游记评论列表
    @GetMapping("/comments")
    public Object comments(TravelCommentQuery qo){
        List<TravelComment> list = commentService.queryByTravelId(qo);
        return JsonResult.success(list);
    }

}
